package com.kai.kaicontroller;

import com.kai.entity.People;
import com.kai.kaiservice.UserService;
import com.kai.kaispringmvc.annotation.*;
import org.apache.commons.lang3.text.StrBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author kai
 * @version 1.0
 * @date 2024/7/10
 * @Description
 */
@kaiController
public class kaiHandler {

    @kaiAutowired
    private UserService userService;

    private List<People> peopleList;

    @kaiRequestMapping(value = "/user/list")
    public void listpeople(HttpServletRequest request, HttpServletResponse
            response) {
        response.setContentType("text/html;charset=utf-8");

        StrBuilder content = new StrBuilder("<h1>信息列表</h1>");

        List<People> people_ = userService.list_people();

        content.append("<h1><table border='1px' style='border-collapse:  collapse' border='1px'>");
        for (People people : people_) {
            content.append("<tr><td>" + people.getId() + "</td><td>"
                    + people.getName() + "</td><td>" + people.getSkill()
                    + "</td>");
        }
        content.append("</table></h1>");
        try {
            PrintWriter printWriter = response.getWriter();
            printWriter.write(content.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    /*编写方法，可以列出方法*/
    /*springmvc支持原生的servlet api*/
    @kaiRequestMapping("/list/handler")
    public void listhandler(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("text/html;charset=utf-8");


        StrBuilder content = new StrBuilder("<h1>信息列表</h1>");


        try {  //ctrl+alt+t
            PrintWriter printWriter = response.getWriter();
            printWriter.write("<h1>信息列表</h1>");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //增加方法
    @kaiRequestMapping(value = "/user/find")
    public void fingpeopleName(HttpServletRequest request,
                               HttpServletResponse response,
            /* @kaiRequestParam("name") */String name) {

        response.setContentType("text/html;charset=utf-8");
        System.out.println("-------------------------------------------------------");
        System.out.println("接收到的参数---" + name);
        StrBuilder content = new StrBuilder("<h1>信息列表</h1>");

        List<People> people_ = userService.findPeopleName(name);

        content.append("<h1><table border='1px' style='border-collapse:  collapse' border='1px'>");
        for (People people : people_) {
            content.append("<tr><td>" + people.getId() + "</td><td>"
                    + people.getName() + "</td><td>" + people.getSkill()
                    + "</td>");
        }
        content.append("</table></h1>");
        try {
            PrintWriter printWriter = response.getWriter();
            printWriter.write(content.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //处理用户登录的方法,返回要请求转发或者重定向的字符串
    @kaiRequestMapping("/users/login")
    public String login(HttpServletRequest request,
                        HttpServletResponse response, String userName) {
        request.setAttribute("userName", userName);

        boolean login = userService.Login(userName);
        System.out.println("---------------userName=" + userName);
        if (login) {
            System.out.println("ok");
            return "forward:/Login_ok.jsp";
            // return "redirect:/Login_ok.jsp";
            //         return "/Login_ok.jsp";
        } else {
            System.out.println("error");
            return "forward:/Login_error.jsp";
        }


    }



    /**
     *  //编写方法，返回json数据
     *  目标方法返回的结果是给到springMvc底层反射调用的位置
     *  我们在springMvc底层反射调用的位置，接收到结果并解析即可
     * @param request
     * @param response
     * @return
     */
    @kaiRequestMapping("/users/list/json")
    @kaiResponseBody
    public List<People> listpeopleByjson(HttpServletRequest request,
                                         HttpServletResponse response) {


        List<People> people = userService.list_people();

        return people;
    }



}
